package com.burgeon.backend.SHOP.rest;
/**
 * 修改对应会员openid APP
 */
import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.service.ex.ErrorDataException;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.Base64;
import java.util.HashMap;

public class UserReadAppOpenid extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public UserReadAppOpenid(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */
    public void getData(String id,String openid,String openidV,String nickName) throws Exception {
        String url = "http://"+ip+"/servlets/binserv/Rest";
        System.err.println("id="+id+"openid="+openid+"openidV="+openidV);
        String str = RESULT(Data(id,openid,openidV,nickName),url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)){
            String message = new JSONArray(str).getJSONObject(0).getString("message");
            throw new ErrorDataException("数据错误"+message);
        }

    }

    private MultiValueMap Data(String id,String openid,String openidV,String nickName) {
        HashMap params = new HashMap();

        params.put("ID", id);
        params.put("partial_update","true");

        params.put("table", "C_VIP");//表名

        params.put("OPENID2", "'"+openid+"'");
        params.put("OPENID","'"+openidV+"'");
        if (null != nickName){
            params.put("WXCODE","'"+strConvertBase(nickName)+"'");
        }

        return DATA("ObjectModify", params,account,password);
    }

    //转码
    public static String strConvertBase(String str) {
        if(null != str){
            Base64.Encoder encoder = Base64.getEncoder();
            return encoder.encodeToString(str.getBytes());
        }
        return null;
    }
    public static void main(String[] args) {
        UserReadAppOpenid g = new UserReadAppOpenid("39.99.239.133:55322","nea@burgeon.com.cn","mgg55321");
        try {
            g.getData("599050","123","123","");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
